﻿using Desktop.Interfaces;
using System;
using System.Runtime.Serialization;

namespace Desktop.Library
{
    [Serializable()]
    public class Chapter : ContentItem<Chapter>
    {
        #region Variables

        private string _title;
        private string _summary;
        private ObservableList<IContentItem> _sections;

        #endregion

        #region Constructors

        public Chapter()
            : base()
        {
            Title = string.Empty;
            Summary = string.Empty;
            Sections = new ObservableList<IContentItem>();
        }

        public Chapter(Chapter chapter)
            : base(chapter)
        {
            if (chapter == null)
            {
                return;
            }
            Title = chapter.Title;
            Summary = chapter.Summary;
            Sections = chapter.Sections.Clone();
        }

        public Chapter(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
            Title = info.GetString("Title");
            Summary = info.GetString("Summary");
            Sections = info.GetValue("Sections", typeof(ObservableList<IContentItem>)) as ObservableList<IContentItem>;
        }

        #endregion

        #region Properties

        public string Title
        {
            get
            {
                return _title;
            }
            set
            {
                if (_title != value)
                {
                    PropertyHasChanged("Title");
                }
                _title = value;
            }
        }

        public string Summary
        {
            get
            {
                return _summary;
            }
            set
            {
                if (_summary != value)
                {
                    PropertyHasChanged("Summary");
                }
                _summary = value;
            }
        }

        public ObservableList<IContentItem> Sections
        {
            get
            {
                return _sections;
            }
            set
            {
                if (_sections != value)
                {
                    PropertyHasChanged("Sections");
                }
                _sections = value;
            }
        }

        #endregion

        #region Methods

        public override string ToString()
        {
            return Title;
        }

        public override void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("Title", Title);
            info.AddValue("Summary", Summary);
            info.AddValue("Sections", Sections);
            base.GetObjectData(info, context);
        }

        #endregion
    }
}